package com.compass.doctor.ui.home.viewmodel

import androidx.lifecycle.MutableLiveData
import com.compass.framework.model.Consumable
import com.compass.framework.model.ConsumableOrder
import com.compass.framework.model.CreateConsumable
import com.compass.framework.model.MedicalCareOrder
import com.compass.framework.model.NurseFinish
import com.compass.framework.toast.TipsToast
import com.compass.network.callback.IApiErrorCallback
import com.compass.network.manager.ApiManager
import com.compass.network.viewmodel.BaseViewModel
import com.google.gson.JsonObject
import okhttp3.MediaType
import okhttp3.MediaType.Companion.toMediaType
import okhttp3.MediaType.Companion.toMediaTypeOrNull
import okhttp3.MultipartBody
import okhttp3.RequestBody
import okhttp3.RequestBody.Companion.asRequestBody
import java.io.File


/**
 * Description： 医护上门viewModel
 * Date:2023/12/31 11:18
 */
class MedicalCareViewModel : BaseViewModel() {

    val careLiveData: MutableLiveData<MutableList<MedicalCareOrder>?> = MutableLiveData()
    val afterSaleData: MutableLiveData<MutableList<MedicalCareOrder>?> = MutableLiveData()
    val messageLiveData: MutableLiveData<MutableList<MedicalCareOrder>?> = MutableLiveData()

    val orderDetailsLiveData: MutableLiveData<MedicalCareOrder> = MutableLiveData()
    val updateLiveData: MutableLiveData<String?> = MutableLiveData()
    val departLiveData: MutableLiveData<Boolean> = MutableLiveData()//出发
    val reachLiveData: MutableLiveData<Boolean> = MutableLiveData()//到达
    val finishLiveData: MutableLiveData<Boolean> = MutableLiveData()//完成
    val callLiveData: MutableLiveData<Boolean> = MutableLiveData()//外呼
    val deleteOrderLiveData: MutableLiveData<Boolean> = MutableLiveData()//删除耗材订单
    val createConsumableLiveData: MutableLiveData<String?> = MutableLiveData() //创建耗材订单成功
    val payCodeLiveData: MutableLiveData<String?> = MutableLiveData() //获取支付二维码
    val consumableLiveData: MutableLiveData<MutableList<Consumable>?> = MutableLiveData()
    val consumableOrderLiveData: MutableLiveData<MutableList<ConsumableOrder>?> = MutableLiveData()


    /**
     * 获取耗材
     */
    fun getConsumable(keyword: String="") {
        launchUIWithResult(responseBlock = {
            ApiManager.api.getConsumable(keyword )
        }, errorCall = object : IApiErrorCallback {
            override fun onError(code: Int?, error: String?) {
                TipsToast.showTips(error)
                consumableLiveData.value=null
            }
        }, successBlock = {
            if (it.isNullOrEmpty()){
                consumableLiveData.value=null
            }else{
                consumableLiveData.value=it
            }
        })
    }


    /**
     * 获取耗材订单
     */
    fun getConsumableOrder(orderId: String) {
        launchUIWithResult(responseBlock = {
            ApiManager.api.getConsumableOrder(orderId)
        }, errorCall = object : IApiErrorCallback {
            override fun onError(code: Int?, error: String?) {
                TipsToast.showTips(error)
                consumableOrderLiveData.value=null
            }
        }, successBlock = {
            consumableOrderLiveData.value=it
        })
    }

    /**
     * 删除耗材订单
     */
    fun deleteConsumableOrder(orderId: String) {
        launchUIWithResult(responseBlock = {
            ApiManager.api.deleteConsumableOrder(orderId)
        }, errorCall = object : IApiErrorCallback {
            override fun onError(code: Int?, error: String?) {
                TipsToast.showTips(error)
            }
        }, successBlock = {
            it?.let {
                deleteOrderLiveData.value = true
            }

        })
    }


    /**
     * 耗材订单二次支付
     */
    fun consumableOrderPay(orderId: String) {
        launchUIWithResult(responseBlock = {
            ApiManager.api.consumableOrderPay(orderId)
        }, errorCall = object : IApiErrorCallback {
            override fun onError(code: Int?, error: String?) {
                TipsToast.showTips(error)
                payCodeLiveData.value=null
            }
        }, successBlock = {
            payCodeLiveData.value=it

        })
    }

    /**
     * 创建订单
     */
    fun createConsumable(consumable: CreateConsumable) {
        launchUIWithResult(responseBlock = {
            ApiManager.api.createConsumable(consumable)
        }, errorCall = object : IApiErrorCallback {
            override fun onError(code: Int?, error: String?) {
                TipsToast.showTips(error)
                createConsumableLiveData.value=null
            }
        }, successBlock = {
            createConsumableLiveData.value=it
        })
    }


    fun getNurseList(pageNo: Int, pageSize: Int = 10) {
        launchUIWithResult(responseBlock = {
            ApiManager.api.getNurseList(pageNo, pageSize, "1")
        }, errorCall = object : IApiErrorCallback {
            override fun onError(code: Int?, error: String?) {
                TipsToast.showTips(error)
                messageLiveData.value = null
            }
        }, successBlock = {
            if (it == null || it.list.isNullOrEmpty()) {
                messageLiveData.value = null
            } else {
                messageLiveData.value = it.list
            }
        })
    }

    fun getCareNurseList(pageNo: Int, pageSize: Int = 10) {
        launchUIWithResult(responseBlock = {
            ApiManager.api.getNurseList(pageNo, pageSize, "2")
        }, errorCall = object : IApiErrorCallback {
            override fun onError(code: Int?, error: String?) {
                TipsToast.showTips(error)
                careLiveData.value = null
            }
        }, successBlock = {
            if (it == null || it.list.isNullOrEmpty()) {
                careLiveData.value = null
            } else {
                careLiveData.value = it.list
            }
        })
    }

    fun getAfterSaleList(pageNo: Int, pageSize: Int = 10) {
        launchUIWithResult(responseBlock = {
            ApiManager.api.getNurseList(pageNo, pageSize, "3")
        }, errorCall = object : IApiErrorCallback {
            override fun onError(code: Int?, error: String?) {
                TipsToast.showTips(error)
                afterSaleData.value = null
            }
        }, successBlock = {
            if (it == null || it.list.isNullOrEmpty()) {
                afterSaleData.value = null
            } else {
                afterSaleData.value = it.list
            }
        })
    }


    fun getMedicalCareOrderDetails(orderId: String?) {
        launchUIWithResult(responseBlock = {
            ApiManager.api.getMedicalCareOrderDetails(orderId)
        }, errorCall = object : IApiErrorCallback {
            override fun onError(code: Int?, error: String?) {
                TipsToast.showTips(error)
            }
        }, successBlock = {
            it?.let {
                orderDetailsLiveData.value = it
            }
        })
    }


    /**
     * 出发
     */
    fun nurseDeparted(id: String, lat: Double, lot: Double) {
        val params = JsonObject().apply {
            addProperty("id", id)
            addProperty("lat", lat)
            addProperty("lot", lot)
        }
        launchUIWithResult(responseBlock = {
            ApiManager.api.nurseDeparted(params)
        }, errorCall = object : IApiErrorCallback {
            override fun onError(code: Int?, error: String?) {
                TipsToast.showTips(error)
            }
        }, successBlock = {
            it?.let {
                departLiveData.value = true
            }

        })
    }

    /**
     * 到达
     */
    fun nurseArrive(id: String, lat: Double, lot: Double) {
        val params = JsonObject().apply {
            addProperty("id", id)
            addProperty("lat", lat)
            addProperty("lot", lot)
        }
        launchUIWithResult(responseBlock = {
            ApiManager.api.nurseArrive(params)
        }, errorCall = object : IApiErrorCallback {
            override fun onError(code: Int?, error: String?) {
                TipsToast.showTips(error)
            }
        }, successBlock = {

            it?.let {
                reachLiveData.value = true
            }
        })
    }


    /**
     * 完成
     */
    fun nurseFinish(nurseFinish: NurseFinish) {
        launchUIWithResult(responseBlock = {
            ApiManager.api.nurseFinish(nurseFinish)
        }, errorCall = object : IApiErrorCallback {
            override fun onError(code: Int?, error: String?) {
                TipsToast.showTips(error)
            }
        }, successBlock = {
            it?.let {
                finishLiveData.value = true
            }
        })
    }


    /**
     * 电话外呼
     */
    fun nurseCall(id: String?) {
        launchUIWithResult(responseBlock = {
            ApiManager.api.nurseCall(id)
        }, errorCall = object : IApiErrorCallback {
            override fun onError(code: Int?, error: String?) {
                TipsToast.showTips(error)
            }
        }, successBlock = {
            it?.let {
                callLiveData.value = true
            }
        })
    }


    fun upload(file: File) {
        //File转RequestBody
        val fileBody = file.asRequestBody("image/jpg".toMediaType())
        //将文件转化为MultipartBody.Part
        //第一个参数：上传文件的key；第二个参数：文件名；第三个参数：RequestBody对象
        val filePart = MultipartBody.Part.createFormData("file", file.name, fileBody)
        launchUIWithResult(responseBlock = {
            ApiManager.api.upload(filePart)
        }, errorCall = object : IApiErrorCallback {
            override fun onError(code: Int?, error: String?) {
                updateLiveData.value=null
            }
        }, successBlock = {
            updateLiveData.value=it
        })
    }
}